<template>
  <div>
    <el-dialog class="abow_dialog"
               :visible.sync="centerDialogVisible"
               center
               :title="title">
      <div style="text-align:right;color:darkorange;margin: auto; width: max-content; max-height: 100%">
        课程名称：<el-input v-model="in_lessonName"
                  class="input3"
                  placeholder="课程名称"
                  style="width: 200px"></el-input>
        <br>
        实验室类型：<template>
          <el-select v-model="in_labType"
                     class="input3"
                     clearable
                     style="width: 200px;"
                     placeholder="实验室类型">
            <el-option v-for="item in labOptions"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </template><br><br>
        上课班级：<template>
          <el-select v-model="in_stuClass"
                     class="input3"
                     clearable
                     style="width: 200px;"
                     placeholder="上课班级">
            <el-option v-for="item in clazzOption"
                       :key="item.clazz"
                       :label="'班级:'+item.clazz+' 人数:'+item.count"
                       :value="'班级:'+item.clazz+' 人数:'+item.count">
            </el-option>
          </el-select>
        </template><br><br>
        起始周次：<el-input v-model="in_strWeek"
                  class="input3"
                  placeholder="起始周次"
                  style="width: 200px"></el-input>
        <br>
        结束周次：<el-input v-model="in_endWeek"
                  class="input3"
                  placeholder="结束周次"
                  style="width: 200px"></el-input>
        <br>
        上课节次：<template>
          <el-select v-model="in_section"
                     class="input3"
                     clearable
                     style="width: 200px;"
                     placeholder="节次">
            <el-option v-for="item in options"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </template><br /><br />
        <div style="text-align:center">
          <el-button class="botton"
                     @click="centerDialogVisible = false">取 消</el-button>
          <el-button class="botton"
                     @click="insertConfirm()">确 定</el-button>
        </div>

      </div>
    </el-dialog>
    <el-dialog class="abow_dialog"
               :visible.sync="centerDialogVisible2"
               center
               :title="title">
      <div style="color: darkorange;text-align: right;margin: auto; width: max-content; max-height: 100%">
        <table style="text-align: left;">
          <tr>
            <td width="180px">教师工号：{{in_userId}}</td>
            <td width="180px">排课学期：{{in_term}}</td>
          </tr>
          <tr>
            <td width="180px">课程名称：{{in_lessonName}}</td>
            <td width="180px">实验室类型：{{in_labTypeString}}</td>
          </tr>
          <tr>
            <td width="180px">开始周：{{in_strWeek}}</td>
            <td width="180px">结束周：{{in_endWeek}}</td>
          </tr>
          <tr>
            <td width="180px">学生班级：{{in_stuClass}}</td>
            <td width="180px">学生数量：{{in_stuNum}}</td>
          </tr>
          <tr>
            <td width="180px">节次：{{in_sectionString}}</td>
            <td width="180px">申请状态：{{in_stateString}}</td>
          </tr>
        </table>
      </div>
    </el-dialog>
    <div style="color: darkorange; font-size: small; height: 30px">
      <el-input v-model="to_term"
                class="input"
                placeholder="申请学期"
                style="width: 100px"></el-input>&nbsp;
      <el-input v-model="to_lessonName"
                class="input"
                placeholder="课程名称"
                style="width: 100px"></el-input>&nbsp;
      <el-input v-model="to_stuClass"
                class="input"
                placeholder="上课班级"
                style="width: 100px"></el-input>&nbsp;
      <template>
        <el-select v-model="to_state"
                   class="input"
                   clearable
                   style="width: 100px;"
                   placeholder="申请状态">
          <el-option v-for="item in stateOptions"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>
      </template>&nbsp;
      <el-button class="botton"
                 @click="load()">搜索</el-button>
      <el-button class="botton"
                 @click="insert()">申请 <i class="el-icon-circle-plus"></i></el-button>
    </div>
    <div style="margin-top: 8px; margin-bottom: 15px"></div>
    <div class="big">
      <el-table :data="tableData"
                :row-style="{ color: 'darkorange', height: '60px' }"
                border
                fit
                style="width: 100%">
        <el-table-column label="申请学期"
                         sortable
                         prop="term"></el-table-column>
        <el-table-column label="课程名称"
                         prop="lessonName"> </el-table-column>
        <el-table-column label="上课班级"
                         sortable
                         prop="stuClass"> </el-table-column>
        <el-table-column label="申请状态"
                         prop="stateString"> </el-table-column>
        <el-table-column fixed="right"
                         label="查看/修改"
                         width="100px">
          <template slot-scope="scope">
            <div class="edit_delete">
              <el-button circle
                         icon="el-icon-view"
                         type="primary"
                         @click="showInfo(scope.row)"></el-button>
              <el-button circle
                         v-if="!scope.row.state"
                         icon="el-icon-edit"
                         type="warning"
                         @click="editInfo(scope.row)"></el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="padding: 10px 0; text-align: center">
      <el-pagination :current-page="page"
                     :total="total"
                     background
                     class="white-list-tool"
                     layout="total,prev, pager, next, jumper"
                     @current-change="handleCurrentChange">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  computed: mapState(["user", "term", "token"]),
  data () {
    return {
      labOptions: [{
        value: '0',
        label: '软件'
      }, {
        value: '1',
        label: '硬件'
      }, {
        value: '2',
        label: '网络'
      }],
      stateOptions: [{
        value: "0",
        label: '未排课'
      }, {
        value: "1",
        label: '已排课'
      }],
      options: [{
        value: 0,
        label: '1~2'
      }, {
        value: 1,
        label: '3~5'
      }, {
        value: 2,
        label: '6~7'
      }, {
        value: 3,
        label: '8~9'
      }, {
        value: 4,
        label: '10~12'
      }, {
        value: 5,
        label: '13~15'
      }],
      title: "",
      tableData: [],
      total: 0,
      page: 1,
      centerDialogVisible: false,
      centerDialogVisible2: false,
      insertOrUpdate: true,

      in_userId: '',
      in_lessonName: '',
      in_labType: '',
      in_stuClass: '',
      in_stuNum: '',
      in_strWeek: '',
      in_endWeek: '',
      in_section: '',
      in_trId: '',
      in_term: '',
      in_state: '',
      in_stateString: '',
      in_labTypeString: '',
      in_sectionString: '',

      to_lessonName: '',
      to_term: '',
      to_state: '',
      to_stuClass: '',

      multipleSelection: [],
      clazzOption: ''
    };
  },
  methods: {
    insert () {
      this.centerDialogVisible = true
      this.insertOrUpdate = true
      this.title = "申请编写"
      this.clearInput()
    },
    clearInput () {
      this.in_lessonName = ""
      this.in_labType = ""
      this.in_stuClass = ""
      this.in_stuNum = ""
      this.in_strWeek = ""
      this.in_endWeek = ""
      this.in_section = ""
      this.in_trId = ""
      this.in_userId = ""
      this.in_term = ""
      this.in_state = ""
    },
    showInfo (row) {
      this.title = "详细信息"
      this.clearInput()
      this.in_lessonName = row.lessonName
      this.in_stuClass = row.stuClass
      this.in_stuNum = row.stuNum
      this.in_strWeek = row.startWeek
      this.in_endWeek = row.endWeek
      this.in_userId = row.userId
      this.in_term = row.term
      this.in_stateString = row.stateString
      this.in_labTypeString = row.labTypeString
      this.in_sectionString = row.sectionString
      this.centerDialogVisible2 = true;
    },
    editInfo (row) {
      this.title = "修改申请信息"
      this.insertOrUpdate = false
      this.clearInput()
      this.in_lessonName = row.lessonName
      this.in_labType = row.labTypeString
      this.in_stuClass = '班级:' + row.stuClass + ' 人数:' + row.stuNum
      this.in_stuNum = row.stuNum
      this.in_strWeek = row.startWeek
      this.in_endWeek = row.endWeek
      this.in_section = row.section
      this.in_trId = row.trId
      this.in_userId = row.userId
      this.in_term = row.term
      this.in_state = row.state
      this.centerDialogVisible = true;
    },
    async insertConfirm () {
      var list = this.in_stuClass.split(/[,-\s:]/)
      this.in_stuNum = list[3]
      this.in_stuClass = list[1]
      if (this.insertOrUpdate === true) {
        await this.$http.post("/teaReq", {
          term: this.term,
          userId: this.user.userId,
          lessonName: this.in_lessonName,
          labType: this.in_labType,
          stuClass: this.in_stuClass,
          stuNum: this.in_stuNum,
          startWeek: this.in_strWeek,
          endWeek: this.in_endWeek,
          section: this.in_section
        }, { headers: { 'userId': this.token } }).then((res) => {
          if (res.data.success === true) { this.$message("插入成功") }
          else if (res.data.success === false) { this.$message(res.data.errorMsg) }
        });
      }
      if (this.insertOrUpdate === false) {
        if (this.in_labType === '软件') {
          this.in_labType = 0;
        } else if (this.in_labType === '硬件') {
          this.in_labType = 1;
        } else if (this.in_labType === '网络') {
          this.in_labType = 2;
        }
        await this.$http.put("/teaReq", {
          trId: this.in_trId,
          term: this.term,
          userId: this.user.userId,
          lessonName: this.in_lessonName,
          labType: this.in_labType,
          stuClass: this.in_stuClass,
          stuNum: this.in_stuNum,
          startWeek: this.in_strWeek,
          endWeek: this.in_endWeek,
          section: this.in_section
        }, { headers: { 'userId': this.token } }).then((res) => {
          if (res.data.success === true) { this.$message("修改成功") }
          else if (res.data.success === false) { this.$message(res.data.errorMsg) }
        });
      }
      this.centerDialogVisible = false;
      this.load();
    },
    load () {
      this.$http.post("/teaReq/1", {
        term: this.to_term,
        userId: this.user.userId,
        stuClass: this.to_stuClass,
        lessonName: this.to_lessonName,
        state: this.to_state,
        page: this.page,
      }, { headers: { 'userId': this.token } }).then((res) => {
        this.tableData = res.data.data
        this.total = res.data.total
      });
    },
    handleCurrentChange (page) {
      this.page = page;
      this.load();
    },
  },
  created () {
    this.$http.get("/stu/clazz", { headers: { 'userId': this.token } }).then((res) => {
      if (res.data.success === true) {
        this.clazzOption = res.data.data
      }
      else { this.$message(res.data.errorMsg) }
    })
    this.load();
  },
};
</script>


<style scoped>
::v-deep .el-dialog {
  background-color: #525151;
  border: 1px solid darkorange;
}

::v-deep .el-dialog__title {
  color: darkorange;
  caret-color: transparent;
}

.input3 /deep/ .el-input__inner {
  margin-right: 5px;
  background-color: #525151;
  border: 1px solid darkorange;
  color: darkorange;
}

.input /deep/ .el-input__inner {
  background-color: #525151;
  border: 1px solid darkorange;
  color: darkorange;
  width: 100px;
}

.botton {
  color: darkorange;
  background-color: #525151;
  border-color: darkorange;
}

.botton:hover,
.botton:focus {
  background: rgb(255, 200, 133);
  border-color: darkorange;
  color: black;
}

.big {
  border: 1px solid darkorange;
}

::v-deep .el-table th {
  border: 1px solid darkorange !important;
  border-right: none !important;
  border-bottom: none !important;
  background-color: #525151;
}

::v-deep.el-table thead {
  color: darkorange;
  font-weight: 500;
  border: 1px solid darkorange;
}

::v-deep .el-table tr {
  background-color: #525151;
}

::v-deep .el-table td,
.building-top .el-table th.is-leaf {
  border: 1px solid darkorange;
  border-right: none !important;
}

::v-deep .el-table::before {
  border: 1px solid darkorange;
  height: 0;
}

::v-deep .el-table td,
.el-table th {
  text-align: center;
}

.el-input {
  width: 100px;
  margin-bottom: 20px;
}

.el-table {
  border: 1px solid darkorange;
  margin: 0 auto;
  font-size: 15px;
}
</style>